//
//  restViewController.m
//  QuickCare
//
//  Created by 郑永钦 on 16/7/12.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "restViewController.h"
#import <BaiduMapAPI/BMKMapComponent.h>
//定位模块
#import <BaiduMapAPI/BMKLocationService.h>

#import <BaiduMapAPI/BMKBaseComponent.h>
#import <BaiduMapAPI/BMKSearchComponent.h>
#import <MapKit/MapKit.h>
#import <BaiduMapAPI/BMKTypes.h>
#import <BaiduMapAPI/BMKUserLocation.h>

#import <CoreLocation/CoreLocation.h>

@interface restViewController ()<BMKLocationServiceDelegate,BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate,BMKMapViewDelegate>
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

//类似位置管理器, 负责地位服务
@property(nonatomic,strong)BMKLocationService *locationService;
//地理编码
@property(nonatomic,strong)BMKGeoCodeSearch *geocoder;
//@property(nonatomic,strong)BMKMapManager *manager;

//@property(nonatomic, strong)BMKPoiSearch *poiSearch;

@property(nonatomic,assign)int currentPage;

@end

@implementation restViewController

#pragma  mark -懒加载
- (BMKLocationService *)locationService
{
    // 1创建位置服务
    if (_locationService == nil) {
        _locationService = [[BMKLocationService alloc] init];
    }
    return _locationService;
}

//- (BMKMapManager *)manager{
//    if (_manager == nil) {
//        _manager = [[BMKMapManager alloc] init];
//    }
//    return _manager;
//}
//
//-(void)viewDidDisappear:(BOOL)animated{
//    if (_manager == nil) {
//        _manager = [[BMKMapManager alloc] init];
//    }
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mapView.delegate = self;
    [self.locationService startUserLocationService];
    [self.mapView setUserTrackingMode:BMKUserTrackingModeFollow];
    self.mapView.showsUserLocation = YES;
    
    
}


#pragma mark - BMKGeoCodeSearchDelegate
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSLog(@"地址:%@ %f %f",result.address,result.location.longitude,result.location.latitude);
}

#pragma mark - BMKLocationServiceDelegate
// 更新位置触发
// 一秒一次
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"经度:%f 纬度:%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
    BMKGeoCodeSearch *geocoder = [[BMKGeoCodeSearch alloc] init];
    geocoder.delegate = self;
    
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    option.reverseGeoPoint = userLocation.location.coordinate;
    [geocoder reverseGeoCode:option];
    
    // 把用户位置传递给地图
    [self.mapView updateLocationData:userLocation];
}

#pragma mark - BMKGeoCodeSearchDelegate
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    self.locationService.userLocation.title = result.address;
    [self.mapView updateLocationData:self.locationService.userLocation];
    
}


#pragma mark - BMKPoiSearchDelegate
// POI检索返回数据
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
    
    if (poiResult.poiInfoList.count == 0) {
        UIAlertView *mBoxView = [[UIAlertView alloc]
                                 initWithTitle:nil
                                 message:@"附近没有水吧"
                                 delegate:nil
                                 cancelButtonTitle:nil
                                 otherButtonTitles:@"确定", nil];
        [mBoxView show];
    }
    for (BMKPoiInfo *info in poiResult.poiInfoList) {
        NSLog(@"名称%@ 地址%@ 电话%@",info.name,info.address,info.phone);
        
        // 创建大头针数据模型
        BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
        annotation.coordinate = info.pt;
        annotation.title = info.name;
        annotation.subtitle = info.address;
        
        // 地图添加大头针数据模型
        [self.mapView addAnnotation:annotation];
    }
}

- (IBAction)waterBarClick:(id)sender {
    BMKPoiSearch *poiSearch = [[BMKPoiSearch alloc] init];
    poiSearch.delegate = self;
    // 创建POI搜索设置
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init];
    option.location = self.locationService.userLocation.location.coordinate;
    option.pageIndex = 1;
    option.pageCapacity = 50;
    option.keyword = @"水吧";
    option.radius = 2000.0f;
    
    // 开始POI搜索
    [poiSearch poiSearchNearBy:option];
    //[self initPoiSearch];
}


//-(void)initPoiSearch
//{
//    _poiSearch = [[BMKPoiSearch alloc]init];
//    _poiSearch.delegate = self;
//    _currentPage = 0;
//    //附近云检索，其他检索方式见详细api
//    BMKNearbySearchOption *nearBySearchOption = [[BMKNearbySearchOption alloc]init];
//    nearBySearchOption.pageIndex = _currentPage; //第几页
//    nearBySearchOption.pageCapacity = 10;  //最多几页
//    nearBySearchOption.keyword = @"网吧";   //检索关键字
//    nearBySearchOption.location = _locationService.userLocation.location.coordinate; // poi检索点
//    nearBySearchOption.radius = 1000; //检索范围 m
//    BOOL flag = [_poiSearch poiSearchNearBy:nearBySearchOption];
//    if(flag)
//    {
//        NSLog(@"城市内检索发送成功");
//    }
//    else
//    {
//        NSLog(@"城市内检索发送失败");
//    }
//    
//}

- (IBAction)myLocation:(id)sender {
    // 设置地图区域（中心点坐标和经纬度跨度）
    CLLocationCoordinate2D center = self.locationService.userLocation.location.coordinate;
    //设置显示跨度
    CLLocationDegrees lati = 0.02;
    CLLocationDegrees lon = 0.02;
    BMKCoordinateSpan span = {lati,lon};
    BMKCoordinateRegion region ={center,span};

    [self.mapView setRegion:region animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
